package com.yuerlive.rainbow.module.homepage;

import android.content.ClipboardManager;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseActivity;
import com.yuerlive.rainbow.controller.LiveRoomController;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.module.homepage.presenter.HomePageContract;
import com.yuerlive.rainbow.module.homepage.presenter.HomePagePresenter;
import com.yuerlive.rainbow.module.live.LiveRoomActivity;
import com.yuerlive.rainbow.observer.Observerable;
import com.yuerlive.rainbow.observer.ObserverableType;
import com.yuerlive.rainbow.utils.DeviceUtils;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.utils.UserProxy;
import com.yuerlive.rainbow.utils.Utils;
import com.yuerlive.rainbow.widget.CustomDialog;
import com.yuerlive.rainbow.widget.timeselector.TextUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Admin on 2017/7/4.
 */

public class HomePageActivity extends BaseActivity implements HomePageContract.View {
    @BindView(R.id.ivLiveState)
    ImageView ivLiveState;
    @BindView(R.id.tvAnchorName)
    TextView tvAnchorName;
    @BindView(R.id.ivAnchorSex)
    ImageView ivAnchorSex;
    @BindView(R.id.tvID)
    TextView tvID;
    @BindView(R.id.llContent)
    RelativeLayout llContent;
    @BindView(R.id.tvFollowNum)
    TextView tvFollowNum;
    @BindView(R.id.llFollowed)
    LinearLayout llFollowed;
    @BindView(R.id.tvFansNum)
    TextView tvFansNum;
    @BindView(R.id.llFans)
    LinearLayout llFans;
    @BindView(R.id.tvCharmNum)
    TextView tvCharmNum;
    @BindView(R.id.llCharm)
    LinearLayout llCharm;
    @BindView(R.id.tvSendNum)
    TextView tvSendNum;
    @BindView(R.id.llSendNum)
    LinearLayout llSendNum;
    @BindView(R.id.tvSignature)
    TextView tvSignature;
    @BindView(R.id.tvRainbowID)
    TextView tvRainbowID;
    @BindView(R.id.rlGold)
    RelativeLayout rlGold;
    @BindView(R.id.tvConstellation)
    TextView tvConstellation;
    @BindView(R.id.llConstellation)
    LinearLayout llConstellation;
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvFollowed)
    TextView tvFollowed;
    @BindView(R.id.llFollowe)
    LinearLayout llFollowe;
    @BindView(R.id.ivBg)
    ImageView ivBg;
    @BindView(R.id.tvLevel)
    TextView tvLevel;
    private HomePageContract.Presenter presenter;
    private UserInfo info;
    String anchorId;
    private long lastClickTime;
    private boolean isFromLive;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_homepage;
    }

    @Override
    protected void initViews() {
        anchorId = getIntent().getStringExtra("otherId");
        isFromLive = getIntent().getBooleanExtra("isFromLive", false);
        if (!TextUtil.isEmpty(anchorId)) {
            presenter = new HomePagePresenter(this);
            presenter.getHomePageData(anchorId);
        }
        ViewGroup.LayoutParams params = llContent.getLayoutParams();
        params.width = DeviceUtils.deviceWidth(mActivity);
        params.height = params.width;
        llContent.setLayoutParams(params);
    }

    @Override
    public void setPresenter(HomePageContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showResult(BaseData<UserInfo> results) {
        setData(results.getObject());
    }

    @Override
    public void showSubscribeResult(int code, String anchorId) {
        if (code == 0) {
            if (info == null) {
                return;
            }
            info.setIsSubscribe((info.getIsSubscribe() + 1) % 2);
            if (info.getIsSubscribe() == 1) {
                showToast("关注成功");
                info.setFans(info.getFans() + 1);
                Observerable.getInstance().notifyChange(ObserverableType.ATTENTION, 1);
                info.setIsSubscribe(1);
                tvFollowed.setText(mActivity.getResources().getString(R.string.cancle_subscibe_anchor));
                tvFollowed.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                tvFansNum.setText(String.valueOf(info.getFans()));
            } else {
                info.setFans(info.getFans() - 1);
                Observerable.getInstance().notifyChange(ObserverableType.ATTENTION, -1);
                showToast("取消关注成功");
                tvFollowed.setText(mActivity.getResources().getString(R.string.subscribe));
                tvFollowed.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_follow, 0, 0, 0);
                tvFansNum.setText(String.valueOf(info.getFans()));
            }
        } else {
            if (info.getIsSubscribe() == 1) {
                showToast("关注失败");
            } else {
                showToast("取消关注失败");
            }
        }
    }

    private void setData(UserInfo info) {
        if (info == null) {
            return;
        }
        this.info = info;
        GlideUtil.load(mActivity, info.getIcon(), ivBg);
        if (info.getLiveState() == 2) {
            ivLiveState.setImageResource(R.drawable.home_page_gaming);
        } else if (info.getLiveState() == 1 || info.getLiveState() == 3) {
            ivLiveState.setImageResource(R.drawable.home_page_living);
        }
        if (info.getLiveState() == 0 || isFromLive == true) {
            ivLiveState.setVisibility(View.GONE);
        } else {
            ivLiveState.setVisibility(View.VISIBLE);
        }
        tvAnchorName.setText(info.getNickname());
        ivAnchorSex.setImageResource(info.getSex() == 0 ? R.drawable.icon_boy : R.drawable.icon_girl);
        tvID.setText("ID:" + info.getId());
        tvRainbowID.setText(info.getId());
        tvFollowNum.setText(Utils.getFormartNum(info.getSubscribenum()));
        tvFansNum.setText(Utils.getFormartNum(info.getFans()));
        tvCharmNum.setText(Utils.getFormartNum(info.getCharmtotalvalue()));
        tvSendNum.setText(Utils.getFormartNum(Integer.parseInt(info.getSendCharmValue())));
        if (info.getIsSubscribe() == 1) {
            tvFollowed.setText(mActivity.getResources().getString(R.string.cancle_subscibe_anchor));
            tvFollowed.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            tvFollowNum.setText(String.valueOf(info.getSubscribenum()));
        } else {
            tvFollowed.setText(mActivity.getResources().getString(R.string.subscribe));
            tvFollowed.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_follow, 0, 0, 0);
            tvFollowNum.setText(String.valueOf(info.getSubscribenum()));
        }
        if (TextUtil.isEmpty(info.getPersonalsignature())) {
            tvSignature.setText(mActivity.getResources().getString(R.string.empty_sign_tip));
        } else {
            tvSignature.setText(info.getPersonalsignature());
        }
        if (TextUtil.isEmpty(info.getConstellation())) {
            tvConstellation.setText(mActivity.getResources().getString(R.string.no_set));
        } else {
            tvConstellation.setText(info.getConstellation());
        }
        tvLevel.setText(info.getLv() + "");
        Utils.setLevel(tvLevel, info.getLv());
        if (info.getId().equals(UserProxy.getUser().getUserId())) {
            llFollowe.setVisibility(View.GONE);
        } else {
            llFollowe.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.ivBack, R.id.llFollowe, R.id.tvRainbowID, R.id.ivLiveState})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                onBackPressed();
                break;
            case R.id.llFollowe:
                if (!Utils.isFastDoubleClick(lastClickTime)) {
                    lastClickTime = System.currentTimeMillis();
                    presenter.subscribe(anchorId);
                }
                break;
            case R.id.tvRainbowID:
                ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                clipboardManager.setText(tvRainbowID.getText().toString());
                showSnackbar(getRootView(mActivity), "ID已复制", 0);
                break;
            case R.id.ivLiveState:
                Intent i = new Intent();
                i.setClass(mActivity, LiveRoomActivity.class);
                i.putExtra("liveId", info.getLiveId());
                i.putExtra("chatroomId", info.getChatRoomId());
                i.putExtra("liveCover", info.getLiveIcon());
                LiveRoomController.getInstance().enterLiveRoom(mActivity, i);
                break;
        }
    }


}
